package com.example.easy.excel.util;


import com.example.easy.excel.entity.User;
import org.apache.poi.hssf.usermodel.*;

import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author:gujunfeng
 * @time:2021/10/13
 */
public class ExportExcelUtil {


    /**
     * @param title  表格的名字
     * @param heards 前台选中要导出的字段
     * @param list   查询数据库返回所有字段的结果
     * @param response    输出
     * @throws Exception
     */
    public  void exportUtilA(String title, String[] heards, List<?> list,HttpServletResponse response) throws Exception {
        // 创建excel工作薄
        HSSFWorkbook workbook = new HSSFWorkbook();
        // 创建excel表
        HSSFSheet sheet = workbook.createSheet(title);
        // 创建标题行
        HSSFRow titleRow = sheet.createRow(0);
        // 标题行写内容
        for (int i = 0; i < heards.length; i++) {
            String s = heards[i];
            HSSFCell cell = titleRow.createCell(i);
            if ("name".equals(s)){
                //"name","age","birthday"
                cell.setCellValue("姓名");
            }
            if ("age".equals(s)){
                //"name","age","birthday"
                cell.setCellValue("年龄");
            }
            if ("birthday".equals(s)){
                //"name","age","birthday"
                cell.setCellValue("爱好");
            }
            if ("address".equals(s)){
                cell.setCellValue("地址");
            }
            if ("createTime".equals(s)){
                cell.setCellValue("日期");
            }

        }

        // 数据行,从1行开始,共list.size行
        for (int i = 1; i <= list.size(); i++) {
            // 创建行
            HSSFRow listRow = sheet.createRow(i);
            // 数据行单元格,从0开始，共heards.length列
            for (int j = 0; j < heards.length; j++) {
                // 创建数据单元格
                HSSFCell listCell = listRow.createCell(j);
                // 通过反射的方式，将heards元素通过字符串拼接的方式，拼接出实体类相对应的get方法;
                String methodName = "get" + heards[j].substring(0, 1).toUpperCase() + heards[j].substring(1);
                try {
                    // 通过反射拿到类对象，再获取类对象的额methodName这个方法
                    Method declaredMethod = User.class.getDeclaredMethod(methodName, null);
                    // 通过invoke提交对象，执行declaredMethod这个方法
                    // 从List<Student>集合中取出list.get(i - 1)的methodName属性的值;
                    Object result = declaredMethod.invoke(list.get(i - 1));

                    //判断返回值的类型
                    if (result instanceof Date) {
                        //设置日期格式内容
//                        HSSFDataFormat dataFormat = workbook.createDataFormat();
//                        short format = dataFormat.getFormat("yyyy-MM-dd");
                        //HSSFCellStyle cellStyle = workbook.createCellStyle();
//                        String format1 = new SimpleDateFormat("yyyy-MM-dd").format(result);
                        listCell.setCellValue(new SimpleDateFormat("yyyy-MM-dd").format(result));
                        // cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
                        //cellStyle.setDataFormat(format);
                       // listCell.setCellStyle(cellStyle);
                   } else {
                        //设置内容
                        listCell.setCellValue(result.toString());
                    }
                } catch (NoSuchMethodException e) {
                    e.printStackTrace();
                }

            }
        }

         /*// 设置响应头
        response.setContentType("application/vnd.ms-excel");
        String fileName = "用户自定义信息表.xls";
        response.setCharacterEncoding("utf-8");
        response.setContentType("multipart/form-data");
        response.addHeader("Content-Disposition", "attachment;" + fileName);
        workbook.write(response.getOutputStream());*/
//        try {
//            workbook.write(out);
//            out.close();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
        // 设置响应表头
        response.setContentType("application/vnb.ms-excel");
        response.setCharacterEncoding("utf-8");
        response.setContentType("multipart/from-date");
        workbook.write(response.getOutputStream());
        workbook.close();

    }

    private class ALIGN_CENTER {
    }
}

